//
//  ADLog.swift
//  ADKit
//
//  Created by Adoma on 2020/10/22.
//

import OSLog
import SwiftyJSON

/// by ADKit
public struct ADLog {
    
    public static func show() {
        
        if APPManager.shared.isTest == false {
            return
        }
        
        let sheet = ADActionSheet()
        
        let change = ADAction.init(title: "切换环境") {
            self.changeEnv()
        }
        sheet.add(action: change)
        
        let clear = ADAction.init(title: "清除用户信息") {
            NotificationCenter.default.post(name: ADKit.NotificationName.clearUserData, object: nil)
            DataManager.shared.cleanAll()
            self.reboot()
        }
        sheet.add(action: clear)
        
        if let testUrl = APPManager.shared.testUrl, let url = URL.init(string: testUrl) {
            let update = ADAction.init(title: "更新APP") {
                UIApplication.shared.open(url)
            }
            sheet.add(action: update)
        }
        
        sheet.show()
    }
    
    public static func changeEnv() {
        
        let sheet = ADActionSheet()
        
        sheet.title = "当前环境:\n\(APPManager.shared.env.identifer)"
        
        for env in APPManager.shared.envs {
            if env.identifer == APPManager.shared.env.identifer {
                continue
            }
            let action = ADAction.init(title: "\(env.identifer)") {
                APPManager.shared.change(env: env)
                self.reboot()
            }
            sheet.add(action: action)
        }
        
        sheet.show()
        
    }
    
    static func reboot() {
        
        ADToast.show(msg: "请重新打开APP")
        
        DispatchQueue.main.after(time: 1.5) {
            let sy = "s"
            let pend = "pend"
            let mu = "u"
            UIApplication.shared.perform(Selector(sy+mu+sy+pend))
            DispatchQueue.main.after(time: 0.5) {
                exit(0)
            }
        }
    }
}

public struct ADLogger {
    
    let subsystem: String
    let category: String
    
    public init(subsystem: String = Bundle.main.name, category: String) {
        self.subsystem = subsystem
        self.category = category
    }
    
    init(adkit category: Any.Type) {
        self.init(subsystem: "ADKit", category: "\(category)")
    }
    
    let loggingQueue = DispatchQueue(label: "ADKit.ADLogger.Queue")
    let formatter = DateFormatter(format: "yyyy-MM-dd HH:mm:ss.SSS")

    public func log(level: OSLogType, _ items: Any? ...) {
        if #available(iOS 14, *) {
            let start = formatter.string(from: Date()) + "\n"
            loggingQueue.async {
                let logger = Logger(subsystem: subsystem, category: category)
                let message = items.reduce(into: start) { result, item in
                    guard let item = item else {
                        return
                    }
                    result += "\(item)\n"
                }
                logger.log(level: level, "\(message)")
            }
        }
    }
    
    public func debug(_ items: Any? ...) {
        if APPManager.shared.isTest == false {
            return
        }
        log(level: .debug, items)
    }
}
